package com.study.happystudy.service.impl;

import com.study.happystudy.mapper.FileMapper;
import com.study.happystudy.po.File;
import com.study.happystudy.service.FileService;
import com.study.happystudy.tool.KoDoTool;
import com.study.happystudy.tool.PDFConverter;
import jakarta.annotation.Resource;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {
    @Resource
    FileMapper fileMapper;
    @Resource
    KoDoTool koDoTool;

    @Override
    public String uploadFile(String fileName, String fileType, MultipartFile file) {//上传文件
        String headimg = "";
        String url = "";
        try {
            if (fileType.equals("pdf")) {
                headimg = koDoTool.upload(PDFConverter.convertFirstPageToImage(file));
//            fileMapper.uploadFile();
            }
            url = koDoTool.upload(file);
            headimg = headimg.equals("") ? url : headimg;
            fileMapper.uploadFile(headimg, fileName, fileType, url);
            return url;
//        fileMapper.uploadFile(fileName, fileType, );
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }

    }

    @Override
    public void copyFileToCourse(int courseId, int fileId) {
        fileMapper.copyFileToCourse(courseId, fileId);
    }

    @Override
    public void copyFileToUser(int userId, int fileId) {
        fileMapper.copyFileToUser(userId, fileId);
    }

    @Override
    public void deleteFileFromCourse(int courseId, int fileId) {
        fileMapper.deleteFileFromCourse(courseId, fileId);
    }

    @Override
    public void deleteFileFromUser(int userId, int fileId) {
        fileMapper.deleteFileFromUser(userId, fileId);
    }

    @Override
    public File getFileById(int fileId) {
        return fileMapper.getFileById(fileId);
    }

    @Override
    public void deleteFileById(int fileId) {
        fileMapper.deleteFileById(fileId);
    }

    @Override
    public List<File> getFilesByNameInCourse(String fileName, int courseId) {
        return fileMapper.getFilesByNameInCourse(fileName, courseId);
    }

    @Override
    public List<File> getFilesByNameInUser(String fileName, int userId) {
        return fileMapper.getFilesByNameInUser(fileName, userId);
    }

    @Override
    public List<File> getFilesByUserId(int userId) {
        return fileMapper.getFilesByUserId(userId);
    }

    @Override
    public List<File> getFilesByCourseId(int courseId) {
        return fileMapper.getFilesByCourseId(courseId);
    }
}
